package com.zmn.mcc.admin.controller.staff.log;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.zmn.base.common.data.common.dio.company.CompanyDIO;
import com.zmn.base.common.data.common.dio.company.CompanyTypeDIO;
import com.zmn.base.common.data.common.dro.company.CompanyDRO;
import com.zmn.base.plat.engine.common.constant.CompanyConsts;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.AMISListData;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.mcc.model.entity.staff.log.StaffLogQuery;
import com.zmn.mcc.model.entity.staff.log.StaffLogVO;
import com.zmn.mcc.model.entity.staff.log.StaffReqVo;
import org.apache.commons.lang.BooleanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 描述: 员工操作日志查询控制类
 *
 * @auther tanghongjun
 * @since 2019/7/23 16:35
 */
@Controller
@RequestMapping("/staff/log/")
public class StaffLogListController extends StaffLogBaseController {

    @NacosValue(value = "${mcc.staff.log.query.enable:true}", autoRefreshed = true)
    private Boolean enable = true;

    /**
     * 描述: 员工操作日志列表
     *
     * @param staffLogQuery
     * @return mav
     * @auther tanghongjun
     * @since 2019/7/23 17:39
     */
    @RequestMapping(value = "list")
    public ModelAndView list(StaffLogQuery staffLogQuery) {
        if (staffLogQuery.getDeptTwoId() != null && (!Objects.equals(0, staffLogQuery.getDeptTwoId()))) {
            staffLogQuery.setDeptId(staffLogQuery.getDeptTwoId());
        } else if (staffLogQuery.getDeptOneId() != null && (!Objects.equals(0, staffLogQuery.getDeptOneId()))) {
            staffLogQuery.setDeptId(staffLogQuery.getDeptOneId());
        }

        CompanyTypeDIO one = new CompanyTypeDIO();
        one.setType(CompanyConsts.COMPANY_TYPE_SUBSIDIARY);
        one.setChildType(CompanyConsts.COMPANY_TYPE_CHILD_DEFAULT);
        CompanyTypeDIO two = new CompanyTypeDIO();
        two.setType(CompanyConsts.COMPANY_TYPE_HEADOFFICE);
        two.setChildType(CompanyConsts.COMPANY_TYPE_CHILD_DEFAULT);
        List<CompanyTypeDIO> types = Arrays.asList(one, two);
        CompanyDIO baseCompanyQuery = new CompanyDIO();
        baseCompanyQuery.setCompanyTypeQueryList(types);
        baseCompanyQuery.setPageSize(500);
        ResponseDTO<AMISListData<CompanyDRO>> responseDTO = companyListRemoteService.listPageCompanyDROByQuery(baseCompanyQuery);
        List<CompanyDRO> baseCompanyList = new ArrayList<>();
        if (responseDTO.isSuccess()) {
            baseCompanyList.addAll(responseDTO.getData().getItems());
        }

        ModelAndView mav = new ModelAndView("staff/operatorlog/list");
        mav.addObject("baseCompanyList", baseCompanyList);
        mav.addObject("vo", staffLogQuery);
        mav.addObject("platList", GlobalConsts.getPlatList());
        mav.addObject("oneDepts", getOneDeptList());
        mav.addObject("twoDepts", getTwoDeptList());

        return mav;
    }

    /**
     * 描述: 员工操作日志列表
     *
     * @param id
     * @return mav
     * @auther liubodong
     * @since 2019/7/23 17:39
     */
    @RequestMapping(value = "detail")
    public ModelAndView detail(String id) {

        ModelAndView mav = new ModelAndView("staff/operatorlog/detail");

        if (BooleanUtils.isFalse(enable)) {
            return mav;
        }
        StaffLogVO staffLogVO = staffLogBService.findById(id);
        Integer companyId = staffLogVO.getCompanyId();
        ResponseDTO<CompanyDRO> company = companyListRemoteService.getCompanyDROById(companyId);
        if (company.isSuccess()) {
            staffLogVO.setCompanyName(company.getData().getName());
        }
        mav.addObject("staffLogVO", staffLogVO);
        StaffReqVo staffReqVo = staffLogBService.resolveReq(staffLogVO);
        mav.addObject("staffReqVO", staffReqVo);
        return mav;
    }
}
